//
//  CircleViewController.swift
//  HiBook
//
//  Created by Apple on 16/7/27.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import WebKit

class EbookViewController: CommonViewController,WKUIDelegate ,WKNavigationDelegate ,WKScriptMessageHandler  {
    
    var ebookWebView : WKWebView?
    
    var hidden : Bool?
    
    var tabFrame : CGRect?
    
    var frameArray = [CGRect]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "书圈"
        setWebView()
        loadWebView()
        self.view.addSubview(ebookWebView!)
        self.navigationController?.navigationBarHidden = true
        var dateFrom = NSDate(timeIntervalSince1970: 0)
        WKWebsiteDataStore.defaultDataStore().removeDataOfTypes([WKWebsiteDataTypeCookies,WKWebsiteDataTypeDiskCache,WKWebsiteDataTypeMemoryCache,WKWebsiteDataTypeLocalStorage,WKWebsiteDataTypeSessionStorage,WKWebsiteDataTypeOfflineWebApplicationCache], modifiedSince: dateFrom) {
            
        }

        self.tabFrame = self.tabBarController?.tabBar.frame
        
        for view in (self.tabBarController?.view.subviews)!{
            self.frameArray.append(view.frame)
        }
    }
    func doRefresh(){
        self.ebookWebView?.evaluateJavaScript("reload()", completionHandler: { (o, e) in
            print(e)
            print(o)
            print(self.ebookWebView?.canGoBack)
            print(self.ebookWebView?.canGoForward)
        })

    }

    
    func loadWebView(){
    
        let request = NSURLRequest(URL: NSURL(string: "http://\(MAIN_IP):8000/web/ebookView.html")!)
        
        self.ebookWebView?.loadRequest(request)
        
    }
    
    func setWebView(){
        
        let config = WKWebViewConfiguration()
        
        self.ebookWebView = WKWebView(frame: CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) ,configuration: config)
        
        self.ebookWebView?.UIDelegate = self
        
        self.ebookWebView?.navigationDelegate = self
        
        self.ebookWebView?.configuration.userContentController.addScriptMessageHandler(self, name: "responesToPhone")
        
        self.ebookWebView?.allowsBackForwardNavigationGestures = true
        
        self.ebookWebView?.scrollView.scrollEnabled = false
        
    }
    
    func responesToPhone(hidden : Bool){
        print("刷新成功")
        print(self.tabBarController?.viewControllers)
        if hidden {
            self.tabBarController?.tabBar.hidden = true
            for view in (self.tabBarController?.view.subviews)!{
                view.frame = CGRectZero
            }
            
        } else {
            self.tabBarController?.tabBar.hidden = false
            for view in (self.tabBarController?.view.subviews)!{
                var i = 1
                view.frame = frameArray[i]
                i-=1
            }
        }
        
    }
    
    func webViewDidStartLoad(webView: UIWebView) {
        NSLog("启动")
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        NSLog("结束")
    }
    
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        NSLog("err                                                                                                                                                                                                                                                                                  or:\(error)")
    }

    func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
        
        NSLog("方法名:\(message.name) 传回:\(message.body)")
        
        self.hidden = message.body as? Bool
        
        self.responesToPhone(hidden!)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
